
# sqlite 数据库 文章模型
from django.db import models
from django.utils import timezone
from .sqlite_user_model import SqliteUserModel 

# 文章模型
class SqliteArticleModel(models.Model):

    id = models.AutoField(primary_key=True, verbose_name='文章id')  

    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    
    author = models.ForeignKey(
        SqliteUserModel ,
        on_delete=models.CASCADE,
        verbose_name='作者',
        to_field='user_id'  # 显式指定关联到 user_id 字段
    )
    publish_time = models.DateTimeField(
        default=timezone.now,
        verbose_name='发布时间'
    )
    updated_time = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间'
    )
    is_published = models.BooleanField(
        default=False,
        verbose_name='是否发布'
    )

    # 新增字段：阅读次数
    read_count = models.PositiveIntegerField(
        default=0,
        verbose_name='阅读次数'
    )

    # 文章标签（JSON 格式存储）
    tags = models.JSONField(default=list, verbose_name='文章标签')

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'
        ordering = ['-publish_time']

    def __str__(self):
        return self.title